package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserInfoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @Reference
    private UserInfoService userInfoService;
    @Reference
    private UserService userService;
    @GetMapping("/administrator")
    public PageBeanVo administrator(@RequestParam(value = "pageNum",defaultValue = "1")Integer page,
                                    @RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize) {
        System.out.println ("进入分页查询");
        PageBeanVo userInfo = userInfoService.findUserInfo (page, pageSize);
        List<UserInfoVo> userInfoVoList = new ArrayList<> ();
        List<UserInfo> userInfoList = (List<UserInfo>) userInfo.getItems ();
        if (CollectionUtil.isNotEmpty (userInfoList)) {
            for (UserInfo info : userInfoList) {
                UserInfoVo userInfoVo = new UserInfoVo ();
                BeanUtil.copyProperties (info, userInfoVo);
                userInfoVo.setAge (String.valueOf (info.getAge ()));
                String phone = userService.findPhoneByUserId (info.getId ());
                Jedis jedis = new Jedis ("192.168.136.160",6379);
                jedis.select (1);
                String token = jedis.get (ConstantUtil.FREEZE_USER + phone);
                if (token != null) {
                    userInfoVo.setUserStatus ("冻结");
                } else {
                    userInfoVo.setUserStatus ("未冻结");
                }
                userInfoVoList.add (userInfoVo);
            }
        }
        userInfo.setItems (userInfoVoList);

        return userInfo;
    }

    @GetMapping("/run")
    public String run() {
        System.out.println ("你好");
        return "你好啊";
    }

}
